package com.zhoukai.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhoukai.constants.Constants;
import com.zhoukai.domain.DictData;
import com.zhoukai.domain.DictType;
import com.zhoukai.dto.DictTypeDto;
import com.zhoukai.mapper.DictDataMapper;
import com.zhoukai.mapper.DictTypeMapper;
import com.zhoukai.service.DictTypeService;
import com.zhoukai.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Description TODO
 * @Author Mr.ZHOU
 * @Date Created in 2021/12/26 0:15
 */

@Service
public class DictTypeServiceImpl implements DictTypeService {
    @Autowired
    private DictTypeMapper dictTypeMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private DictDataMapper dictDataMapper;

    @Override
    public void dictCacheAsync() {
        //查询所有可用的dictType
        QueryWrapper<DictType> qw=new QueryWrapper<>();
        qw.ge(DictType.COL_STATUS,Constants.STATUS_TRUE);
        List<DictType> dictTypes = this.dictTypeMapper.selectList(qw);
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        for (DictType dictType : dictTypes) {
            QueryWrapper<DictData> qdw=new QueryWrapper<>();
            qdw.ge(DictData.COL_STATUS,Constants.STATUS_TRUE);
            qdw.eq(DictData.COL_DICT_TYPE,dictType.getDictType());
            List<DictData> dictData = dictDataMapper.selectList(qdw);
            valueOperations.set(Constants.DICT_REDIS_PREFIX+dictType.getDictType(), JSON.toJSONString(dictData));
        }
    }


    @Override
    public DataGridView listPage(DictTypeDto dictTypeDto) {
        Page<DictType> page=new Page<>(dictTypeDto.getPageNum(),dictTypeDto.getPageSize());
        QueryWrapper<DictType> qw=new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(dictTypeDto.getDictName()),DictType.COL_DICT_NAME,dictTypeDto.getDictName());
        qw.like(StringUtils.isNotBlank(dictTypeDto.getDictType()),DictType.COL_DICT_TYPE,dictTypeDto.getDictType());
        qw.eq(StringUtils.isNotBlank(dictTypeDto.getStatus()),DictType.COL_STATUS,dictTypeDto.getStatus());
        qw.ge(dictTypeDto.getBeginTime()!=null,DictType.COL_CREATE_TIME,dictTypeDto.getBeginTime());
        qw.le(dictTypeDto.getEndTime()!=null,DictType.COL_CREATE_TIME,dictTypeDto.getEndTime());
        this.dictTypeMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public DataGridView list() {
        QueryWrapper<DictType> qw=new QueryWrapper<>();
        qw.eq(DictType.COL_STATUS, Constants.STATUS_TRUE);
        List<DictType> dictTypes = this.dictTypeMapper.selectList(qw);
        return new DataGridView(Long.valueOf(dictTypes.size()),dictTypes);
    }


    /*
    分析 1，添加   dictId=null dictType是新添加的值
         2，修改  dictId 有值  dictType要修改后的值
     */
    @Override
    public Boolean checkDictTypeUnique(Long dictId, String dictType) {
        dictId = (dictId == null) ? -1L : dictId;  //-1
        QueryWrapper<DictType> qw=new QueryWrapper<>();
        qw.eq(DictType.COL_DICT_TYPE, dictType);
        DictType sysDictType = this.dictTypeMapper.selectOne(qw);  //有能有对象，也可以没 有
        if(dictId==-1L){//添加
            if(null==sysDictType){
                return false;
            }
            if(sysDictType.getDictType().equals(dictType)){
                return true;
            }
        }else{
            if(null==sysDictType){
                return false;
            } else{   // his_pay_type_status 28
                //修改
                if(!dictId.equals(sysDictType.getDictId().longValue())){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public int insert(DictTypeDto dictTypeDto) {
        //1,创建一个DictType  2,把dictTypeDto里面的数据复制到dictType 再去设置创建人，创建时间，修改人修改时间
        DictType dictType=new DictType();
        BeanUtil.copyProperties(dictTypeDto,dictType);
        //设置创建人 创建时间
        dictType.setCreateTime(DateUtil.date());
        dictType.setCreateBy(dictTypeDto.getSimpleUser().getUserName());
        return this.dictTypeMapper.insert(dictType);
    }

    @Override
    public int update(DictTypeDto dictTypeDto) {
        //1,创建一个DictType  2,把dictTypeDto里面的数据复制到dictType 再去设置创建人，创建时间，修改人修改时间
        DictType dictType=new DictType();
        BeanUtil.copyProperties(dictTypeDto,dictType);
        //设置创建人 创建时间
        dictType.setUpdateTime(DateUtil.date());
        dictType.setUpdateBy(dictTypeDto.getSimpleUser().getUserName());
        return this.dictTypeMapper.updateById(dictType);
    }

    @Override
    public int deleteDictTypeByIds(Long[] dictIds) {
        if(null!=dictIds&&dictIds.length>0){
            return this.dictTypeMapper.deleteBatchIds(Arrays.asList(dictIds));
        }
        return 0;
    }

    @Override
    public DictType selectDictTypeById(Long dictId) {
        return this.dictTypeMapper.selectById(dictId);
    }


}

